//
//  XMLParser.m
//  ParkGroup
//
//  Created by harveynash on 7/27/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "XMLParser.h"
#import "LoginState.h"
#import "XMLDataCommon.h"
#import "BrowserAlbum.h"
#import "defines.h"

@implementation XMLParser 
{
    NSInteger iType;
    BrowserAlbum *browserAlbum;
    NSInteger iIndex;
}

@synthesize loginState;
@synthesize xmlData;

@synthesize strCurrentElementValue;

- (XMLParser *) initXMLParser:(NSInteger) parseType
{
	self = [super init];	
	iType = parseType; //  Number of result of result
	switch (iType) {
        case PARSE_LOGIN: {
            loginState = [[LoginState alloc] init];
            loginState.isLoginSuccess = NO;
            break;
        }
        case PARSE_BROWSE_ALBUM: {
            xmlData = [[XMLDataCommon alloc] init];
            iIndex = 0;
            break;
        }
            
        default:
            break;
    }
	return self;
}

- (void) parseBrowserAlbum:(NSString *) elementName {
    if ([elementName isEqualToString:@"TotalPages"]) {
        xmlData.TotalPages = [[NSString stringWithFormat:@"%@", strCurrentElementValue] intValue];
    }
    else if ([elementName isEqualToString:@"PosterAvatarURL"]) {
        browserAlbum.PosterAvatarURL = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"PosterName"]) {
        browserAlbum.PosterName = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"PosterId"]) {
        browserAlbum.PosterId = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"PosterUsername"]) {
        browserAlbum.PosterUsername = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"AlbumAvatarURL"]) {
        browserAlbum.AlbumAvatarURL = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"NumberOfPhotos"]) {
        browserAlbum.NumberOfPhotos = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"AlbumId"]) {
        browserAlbum.AlbumId = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"AlbumTitle"]) {
        browserAlbum.AlbumTitle = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"AlbumDescription"]) {
        browserAlbum.AlbumDescription = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"Item"]) {
        [xmlData.itemList insertObject:browserAlbum atIndex:iIndex];
        iIndex++;
    }
}


- (void) parseLoginTag:(NSString *) elementName
{
    if ([elementName isEqualToString:@"ObjectType"]) {
        loginState.strObjectType = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"ObjectId"]) {
        loginState.strObjectId = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"SessionID"]) {
        loginState.strSessionID = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"AvatarURL"]) {
        loginState.strAvatarURL = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"Name"]) {
        loginState.strName = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"Username"]) {
        loginState.strUsername = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"Status"]) {
        loginState.strStatus = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"Friends"]) {
        loginState.strFriends = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"LastUpdate"]) {
        loginState.strLastUpdate = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"Joined"]) {
        loginState.strJoined = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"MessagesCount"]) {
        loginState.strMessageCount = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
    else if ([elementName isEqualToString:@"ViewCount"]) {
        loginState.strViewCount = [NSString stringWithFormat:@"%@", strCurrentElementValue];
    }
}

- (void) parser:(NSXMLParser *) parser 
didStartElement:(NSString *) elementName 
   namespaceURI:(NSString *) namespaceURI
  qualifiedName:(NSString *) qualifiedName
	 attributes:(NSDictionary *) attributeDict 
{	
	switch (iType) {
        case PARSE_LOGIN: {
            if ([elementName isEqualToString:@"UserLoginResponse"]) {
                loginState.isLoginSuccess = YES;
            }
            else if ([elementName isEqualToString:@"UserLoginErrorResponse"]) {
                loginState.isLoginSuccess = NO;
            }
            else if ([elementName isEqualToString:@"ObjectType"]
                     || [elementName isEqualToString:@"ObjectId"]
                     || [elementName isEqualToString:@"SessionID"]
                     || [elementName isEqualToString:@"AvatarURL"]
                     || [elementName isEqualToString:@"Name"]
                     || [elementName isEqualToString:@"Username"]
                     || [elementName isEqualToString:@"Status"]
                     || [elementName isEqualToString:@"Friends"]
                     || [elementName isEqualToString:@"LastUpdate"]
                     || [elementName isEqualToString:@"Joined"]
                     || [elementName isEqualToString:@"MessagesCount"]
                     || [elementName isEqualToString:@"ViewCount"])
            {
                // [strCurrentElementValue setString:@""];
                strCurrentElementValue = @"";
            }
            break;
        }
        case PARSE_BROWSE_ALBUM: {
            if ([elementName isEqualToString:@"TotalPages"]
                || [elementName isEqualToString:@"PosterAvatarURL"]
                || [elementName isEqualToString:@"PosterName"]
                || [elementName isEqualToString:@"PosterId"]
                || [elementName isEqualToString:@"PosterUsername"]
                || [elementName isEqualToString:@"AlbumAvatarURL"]                
                || [elementName isEqualToString:@"NumberOfPhotos"]
                || [elementName isEqualToString:@"AlbumId"]
                || [elementName isEqualToString:@"AlbumTitle"]
                || [elementName isEqualToString:@"AlbumDescription"])
            {
                // [strCurrentElementValue setString:@""];
                strCurrentElementValue = @"";
            }
            else if ([elementName isEqualToString:@"Item"]) {
                if (browserAlbum) {
                    browserAlbum = nil;
                }
                browserAlbum = [[BrowserAlbum alloc] init];
            }
            break;
        }
            
        // ...
        default:
            break;
    }
}

- (void) parser:(NSXMLParser *) parser foundCharacters:(NSString *) string
{
	/*
    if (!strCurrentElementValue)
	{
		// init the ad hoc string with the value     
		strCurrentElementValue = [[NSMutableString alloc] initWithString:string];
	} 
	else {
		// append value to the ad hoc string		
		strCurrentElementValue =[NSString stringWithFormat:@"", string];
	}
     */
	//NSLog(@"Processing value for : %@", string);
    strCurrentElementValue =[NSString stringWithFormat:@"%@", string];
}


- (void) parser:(NSXMLParser *) parser 
  didEndElement:(NSString *) elementName
   namespaceURI:(NSString *) namespaceURI 
  qualifiedName:(NSString *) qName
{	
	switch (iType) {
        case PARSE_LOGIN: {
            [self parseLoginTag:elementName];
            break;
        }
        case PARSE_BROWSE_ALBUM: {
            [self parseBrowserAlbum:elementName];
            break;
        }
        default:
            break;
    }
}

/*
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    // Handle errors as appropriate for your application.
}
 */

#pragma mark -
#pragma mark Parse method
- (void) doParse:(NSData *) data 
{	
	// create and init NSXMLParser object
	NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:data];	
	
	// set delegate	
	[nsXmlParser setDelegate:self];
	
	// parsing
	BOOL success = [nsXmlParser parse];
	
	if (success) 
	{
		NSLog(@"No errors");	
	} 
	else {
		NSLog(@"Error parsing document!");
	}	
	
	// [nsXmlParser release];	
}

@end
